﻿using CustomPlayerEffects;
using PlayerRoles;
using PluginAPI.Core;
using PluginAPI.Core.Attributes;
using PluginAPI.Enums;
using PluginAPI.Events;
using RoleCustom.Main;
using System;
using Utils;


namespace RoleCustom.Event
{
    /// <summary>
    /// 
    /// </summary>
    public class SCP106Event
    {
        [PluginEvent(ServerEventType.PlayerDamage)]
        public void OnPlayerHurt(PlayerDamageEvent ev)
        {
            if (ev == null) return;
            if (ev.Player == null) return;
            if (ev.Target.Role != RoleTypeId.Scp106) return;

            try
            {
                if (ev.Target == null) return;

                if (ev.Target.ArtificialHealth != 0)
                {
                    ev.Target.Heal(5f);
                    return;
                }

                ev.Target.Heal(15f);
            }
            catch
            {

            }
        }

        [PluginEvent(ServerEventType.PlayerExitPocketDimension)]
        public void OnPlayerExit(PlayerExitPocketDimensionEvent ev)
        {
            var matches = Player.GetPlayers().FindAll(e => e.Role == RoleTypeId.Scp106);
            if (matches.Count == 0) return;

            Array.ForEach(matches.ToArray(), (Player player) => player.Damage(200f, "口袋次元发生幸存者逃脱"));
        }


        [PluginEvent(ServerEventType.PlayerSpawn)]
        public void OnPlayerSpawn(PlayerSpawnEvent ev)
        {
            if (ev.Player.Role != PlayerRoles.RoleTypeId.Scp106) return;

            TimeCoroutine.Delay(0.5f, () =>
            {
                PlayerUtils.SetHP(ev.Player,
                    CodeLoad.Singleton.Config.SCP106Event.Health,
                    CodeLoad.Singleton.Config.SCP106Event.Health);

                PlayerUtils.SetAP(ev.Player,
                    CodeLoad.Singleton.Config.SCP106Event.AHealth,
                    CodeLoad.Singleton.Config.SCP106Event.AHealth);
            });
        }
    }
}
